package airport.user.action;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import airport.user.dao.UserDAO;
import airport.user.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class SignupAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String username;
	private String email;
	private String password;
	private String repassword;

	public String execute() {

		System.out.println("Sign Up Execute");
		System.out.println("username:" + getUsername());
		System.out.println("email:" + getEmail());
		System.out.println("password:" + getPassword());
		System.out.println("password:" + getRepassword());

		User user = new User();
		user.setUserName(username);
		user.setPassword(password);
		user.setEmail(email);
		System.out.println("initiate DAO");
		UserDAO dao = new UserDAO();
		dao.addUser(user);
		return "success";
	}

	public void validate(/*
						 * String username, String email, String password,
						 * String repassword
						 */) {
		// validate username(no other character except a-z/A-Z/0-9 &&less than
		// 45 characters)
		System.out.println("start validation--->");
		String AllChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		// System.out.print("The Validation");
		int PartValid = 0;
		UserDAO check = new UserDAO();
		if (check.findUserByUserName(username) != null) {
			addFieldError("username", "This Username already exists");
		}

		if (username.length() >= 45) {
			addFieldError("username",
					"Invalid Username, your Username has to be less than 45 characters");
		}
		for (int u = 0; u < username.length(); u++) {
			PartValid = 0; // Reset

			if (AllChars.indexOf(username.substring(u, u + 1)) >= 0) {
				PartValid = 1;
			}

			if (PartValid != 1)// Found an invalid character;
			{
				addFieldError("username",
						"Invalid Username, Username can only contain a-z/A-Z/0-9");
			}
		}

		// validate email(should be in the email format and should be unique in
		// the database||less than 45 characters)
		// String email = "checkme@youremail.com";

		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(email);
		if (!matcher.matches()) {
			addFieldError("email", "please enter a valid email address");
		}
		System.out.println("pass email patten check");
		UserDAO check2 = new UserDAO();
		if (check2.findUserByEmail(email) != null) {
			addFieldError("email", "This Email address already exists");
		}
		System.out.print("pass unique email check! ");

		// check password
		if (password.length() < 8) {
			addFieldError("password",
					"Your Password Needs to Have at least 8 Characters");
		}
		if (!(repassword.equals(password))) {
			addFieldError("repassword", "Your Passwords Do Not Match");
		}
		System.out.println("end validation!");

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

}
